package com.yonyou.common.service.messagecenter.controller;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.yonyou.common.service.messagecenter.rabbitmq.controller.MessageCenterController;
import com.yonyou.common.service.messagecenter.rabbitmq.entity.TtMessageHistory;
import com.yonyou.common.service.messagecenter.rabbitmq.entity.TtMessageHistoryConsumer;
import com.yonyou.common.service.messagecenter.rabbitmq.entity.TtResendMessage;
import com.yonyou.common.service.messagecenter.rabbitmq.service.MessageHistoryConsumerService;
import com.yonyou.common.service.messagecenter.rabbitmq.service.MessageHistoryService;
import com.yonyou.common.service.messagecenter.rabbitmq.service.ResendMessageService;

/**
 * 消息页面
 * 
 * @author BENJAMIN
 *
 */
@CrossOrigin
@Controller
@RequestMapping("/message")
public class MessageController {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private MessageHistoryService messageHistoryService;
	
	@Autowired
	private MessageHistoryConsumerService messageHistoryConsumerService;

    
	
	@Autowired
	private ResendMessageService resendMessageService;


    @Autowired
    private MessageCenterController messageCenterController;

//    @RequestMapping("/text")
//    public String text(){
//        String s = messageCenterController.sendWithExchange("{\"body\":\"1\"}", "event_10061002", "100", "55667715f42183f1f541ef055e376cd7", "1490668194403", "", "1490668194403");
//        System.out.println(s);
//        return s;
//    }
    /**
     * 消息
     *
     * @return
     */

    @RequestMapping("/messages")
    public String messages() {
    	logger.info("----------------message,messages");
        return "/message/static/html/messages.html";
    }

    @RequestMapping("/losts")
    public String losts() {
    	logger.info("----------------message,messages");
        return "/message/static/html/losts.html";
    }
    
    
    /**
     * 消费历史
     * 
     * @return
     */
    @RequestMapping("/consumers")
    public String consumers() {
        return "/message/static/html/consumers.html";
    }
    
    /**
     *	重发历史
     * 
     * @return
     */
    @RequestMapping("/resends")
    public String resends() {
        return "/message/static/html/resends.html";
    }
    
    /**
     * 消息历史
     * 
     * @param page
     * @param pageSize
     * @param messageContent
     * @param sign
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("messages.json")
    public Map<String, Object> getMessageLogs(Integer page,Integer pageSize,String messageContent,String sign) throws Exception{
    	PageInfo<TtMessageHistory> logpage = messageHistoryService.getAllMessageByPage(page==null?1:page,pageSize==null?10:pageSize,messageContent,sign);
        logger.debug("logpage===="+logpage);
    	Map<String, Object> map = new HashMap<String, Object>();
        map.put("page", logpage);
        map.put("pages", logpage.getPages());
        return map;
    }
    @ResponseBody
    @RequestMapping("losts.json")
    public Map<String, Object> getLostLogs(Integer page,Integer pageSize,String messageContent,String sign) throws Exception{
    	PageInfo<TtMessageHistory> logpage = messageHistoryService.getAllLostMessageByPage(page==null?1:page,pageSize==null?10:pageSize,messageContent,sign);
        logger.debug("logpage===="+logpage);
    	Map<String, Object> map = new HashMap<String, Object>();
        map.put("page", logpage);
        map.put("pages", logpage.getPages());
        return map;
    }
    
    /**
     * 消费历史
     * 
     * @param page
     * @param pageSize
     * @param messageContent
     * @param sign
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("consumers.json")
    public Map<String, Object> getConsumerHistory(Integer page,Integer pageSize,String sign) throws Exception{
    	PageInfo<TtMessageHistoryConsumer> logpage = messageHistoryConsumerService.getAllConsumersByPage(page==null?1:page,pageSize==null?10:pageSize,sign);
        logger.debug("logpage===="+logpage);
    	Map<String, Object> map = new HashMap<String, Object>();
        map.put("page", logpage);
        map.put("pages", logpage.getPages());
        return map;
    }
    
    /**
     * 获取重发表的记录
     * 
     * @param page
     * @param pageSize
     * @param sign
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("resends.json")
    public Map<String, Object> getResendsHistory(Integer page,Integer pageSize,String sign) throws Exception{
    	PageInfo<TtResendMessage> logpage = resendMessageService.getAllResendsByPage(page==null?1:page,pageSize==null?10:pageSize,sign);
        logger.debug("logpage===="+logpage);
    	Map<String, Object> map = new HashMap<String, Object>();
        map.put("page", logpage);
        map.put("pages", logpage.getPages());
        return map;
    }
    
    
    /**
     * 重置重试次数
     * 
     * @param resendId
     * @return
     */
    @ResponseBody
    @RequestMapping("/reset")
    public Object resetResends(int resendId) {
    	Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", -1);
        try {
        	TtResendMessage resend = new TtResendMessage();
        	resend.setId(resendId);
        	resend.setTryCount(0);
            
        	resendMessageService.updateRetryCount(resend);
            map.put("status", 0);
            map.put("msg","重试成功!");
        } catch (Exception e) {
        	logger.error("重试失败",e);
            //e.printStackTrace();
            map.put("msg", "系统错误,请联系管理员!"+e.getMessage());
        }
        return map;
    }
    
    /**
     * 重置重试次数
     * 
     * @param resendId
     * @return
     */
    @ResponseBody
    @RequestMapping("/resetNum")
    public Object resetPost(@RequestBody String body) {
    	Map m = (Map<String,Object>)JSON.parse(body);
    	Integer resendId =  Integer.parseInt(m.get("resendId").toString());
       
    	Map<String,Object> map = new HashMap<String,Object>();
        try {
        	TtResendMessage resend = new TtResendMessage();
        	resend.setId(resendId);
        	resend.setTryCount(0);
            
        	resendMessageService.updateRetryCount(resend);
            map.put("status", 0);
            map.put("msg","重置成功!");
        } catch (Exception e) {
        	logger.error("重置失败",e);
            //e.printStackTrace();
            map.put("msg", "系统错误,请联系管理员!"+e.getMessage());
        }
        return map;
    }
}
